package com.leyou.gateway.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PrivateKey;
import java.security.PublicKey;


@Data
@Slf4j
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {
    /**
     * 公钥地址
     */
    private String pubKeyPath;



    private PublicKey publicKey;



    //用来读取yml文件中关于user用户的信息
    private UserTokenInfo user = new UserTokenInfo();

    @Data
    public class UserTokenInfo {
        private String cookieName;
    }


    //用来读取yml文件中关于app服务的信息
    private AppTokenInfo app = new AppTokenInfo();

    @Data
    public class AppTokenInfo {
        private String headerName;
        private Long id;
        private String secret;
    }


    //afterPropertiesSet 等待时机，执行，当属性加载赋值完毕后执行
    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            // 获取公钥和私钥
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            log.error("初始化公钥失败！", e);
            throw new RuntimeException(e);
        }
    }

}